import { guideList, tabListData, getPlcyInfor } from '@/services/guide';
import { message } from 'antd';

export default {
  state: {
    formFieldsValue: {},
    activeKey: null,
    tabList: [],
    data: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    },
  },

  effects: {
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(guideList, payload); // 请求
      const {
        pageArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList', // 处理后台返回数据
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      } else {
        message.warning(response.message || '服务端错误');
      }
    },
    // 切换列表
    *tabList({ payload }, { call, put, select }) {
      const response = yield call(tabListData, payload);
      const plcyId = yield select(state => state.routing.location.query.id);
      if (response.success) {
        if (plcyId) {
          yield put({
            type: 'tabListDataDetail',
            payload: response.data,
          });
          const res = yield call(getPlcyInfor, { plcyId });
          if (res.success) {
            yield put({
              type: 'getPlcyInfor',
              payload: res.data,
            });
          } else {
            message.warning(res.message || '服务端错误');
          }
        } else {
          yield put({
            type: 'tabListData',
            payload: response.data,
          });
        }
      }
      return response.success;
    },
  },

  reducers: {
    // 设置页签状态
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: {
            total: data.totalCount,
            current: currentPage,
            pageSize,
          },
        },
      };
    },
    // 获取信息
    getPlcyInfor(state, { payload }) {
      const newPayload = payload;
      Object.keys(newPayload).forEach(key => {
        if (key === 'affix') {
          newPayload[key] = newPayload[key] ? newPayload[key].split(',') : [];
        } else if (key === 'type') {
          let typeArr = '';
          state.tabList.forEach(c => {
            newPayload[key].split(',').forEach(i => {
              if (Number(c.itemCode) === Number(i)) {
                typeArr += `${c.itemName},`;
                newPayload[key] = typeArr.substr(0, typeArr.length - 1);
              }
            });
          });
        }
      });

      return {
        ...state,
        ...payload,
      };
    },
    // 获取table
    tabListData(state, { payload }) {
      const insArr = payload.BIZ_INS_TYPE;
      insArr.push(payload.BIZ_ACCU_TYPE[0]);
      return {
        ...state,
        activeKey: insArr[0].itemCode,
        tabList: insArr,
      };
    },
    // 获取页签
    tabListDataDetail(state, { payload }) {
      const insArr = payload.BIZ_INS_TYPE;
      insArr.push(payload.BIZ_ACCU_TYPE[0]);
      return {
        ...state,
        tabList: insArr,
      };
    },
  },
};
